package com.liting.uitest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liting.uitest.entity.FileInfo;
import com.liting.uitest.mapper.FileInfoMapper;
import com.liting.uitest.model.BaseRes;
import com.liting.uitest.model.ReactionResult;
import com.liting.uitest.service.FileInfoService;
import com.liting.uitest.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {
    @Override
    public BaseRes<FileInfo> uploadFile(MultipartFile multipartFile) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        String folderName = df.format(new Date());

        String originalFilename = multipartFile.getOriginalFilename();

        String uploadPath = FileUtil.getUploadDate(multipartFile, folderName);

        // 数据库保存文件信息
        FileInfo sysFile = new FileInfo();

        sysFile.setFileName(originalFilename);
        sysFile.setFilePath(uploadPath);
        sysFile.setUploadTime(new Date());
        sysFile.setType(FileInfo.FileType_Upload);

        baseMapper.insert(sysFile);


        return new BaseRes<FileInfo>(sysFile);
    }


    @Override
    public FileInfo getFile(Long id) {
        FileInfo sysFile = baseMapper.selectById(id);

        return sysFile;


        //HttpServletResponse response = CommUtils.getResponse();

//        try {
//
//            FileInputStream inputStream = new FileInputStream(uploadPath + path);
//
//            InputStream fis = new BufferedInputStream(inputStream);
//            byte[] buffer = new byte[fis.available()];
//            fis.read(buffer);
//            fis.close();
//
//            CommUtils.setResponseHeader(response, name);
//            OutputStream os = response.getOutputStream();
//            OutputStream toClient = new BufferedOutputStream(os);
//            toClient.write(buffer);
//            toClient.flush();
//            toClient.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public Page<FileInfo> getFileList(Integer pageNum, Integer pageSize, String fileName, String uploadDate) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("upload_time");
        if (fileName != null) {
            queryWrapper.like("file_name", fileName);
        }

        if (uploadDate != null) {
            try {
                DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                Date queryDate = df.parse(uploadDate + "000000");
                queryWrapper.gt("upload_time", queryDate);
            } catch (Exception ex) {
                log.error("getFileList format date error", ex);
            }
        }

        Page<FileInfo> list = this.page(new Page<>(pageNum, pageSize), queryWrapper);

        return list;
    }


}
